package com.anlu.base.datasource.config;

import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import org.aopalliance.intercept.MethodInvocation;

public class LastParamDsProcessor extends DsProcessor {

    private static final String LAST_PREFIX = "#last";

    @Override
    public boolean matches(String key) {
        if(key.startsWith(LAST_PREFIX)){
            DynamicDataSourceContextHolder.clear();
            return true;
        }
        return false;
    }

    @Override
    public String doDetermineDatasource(MethodInvocation invocation, String key) {
        Object[] arguments = invocation.getArguments();

        return String.valueOf(arguments[arguments.length - 1]);
    }
}
